<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">首页</div>
    </nav-bar>
    <tab-control
      :titles="titles"
      @tabCick="tabCick"
      ref="tabControl2"
      class=".tab-control"
      v-show="showTabControl"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperLoad="swiperLoad" />
      <home-recommend :recommends="recommends" />
      <home-feature></home-feature>
      <tab-control :titles="titles" @tabCick="tabCick" ref="tabControl" />
      <goods-list :goods="showGoods"></goods-list>
    </scroll>
    <back-top @click.native="backTop" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import NavBar from "components/common/navBar/NavBar.vue";
import HomeSwiper from "../Home/childrenComponents/HomeSwiper";
import HomeRecommend from "../Home/childrenComponents/HomeRecommend";
import HomeFeature from "../Home/childrenComponents/HomeFeature";
import TabControl from "../../components/content/TabControll";
import GoodsList from "../Home/childrenComponents/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import BackTop from "components/content/BackTop";

import { getHomeMultidata, getHomeGoods } from "network/home";
import { debounce } from "common/utils";
export default {
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommend,
    HomeFeature,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      titles: [],
      currentType: "pop",
      isShowBackTop: false,
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      offsetTop: 0,
      showTabControl: false,
      saveY: 0,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  created() {
    this.fetchHomeMultidata();
    this.fetchHomeGoods("pop");
    this.fetchHomeGoods("new");
    this.fetchHomeGoods("sell");
  },
  mounted() {
    //防抖函数
    const refresh = debounce(this.$refs.scroll.refresh, 1000);
    //监听图片加载完成
    this.$bus.$on("itemIamgeLoad", () => {
      //下拉加载
      refresh();
    });
  },
  destroyed() {
    console.log("destroyed");
  },
  activated() {
    console.log('home enter 设置位置');
    // console.log(this.saveY);
    // this.$refs.scroll.scrollTo(0, this.saveY, 0);
    // this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    console.log('home leave 记录位置');
    this.saveY = this.$refs.scroll.scroll.y;
  },
  methods: {
    //获取首页banner和推介数据
    fetchHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    //获取首页商品数据
    fetchHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.titles = Array.from(res.data.filter.list);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;

        //完成上拉加载更多
        this.$refs.scroll && this.$refs.scroll.finishPullUp();
      });
    },
    //点击tab获取相应的数据
    tabCick(type) {
      this.currentType = type;
    },

    //返回首页
    backTop() {
      this.$refs.scroll.scrollTo(0, 0, 5000);
    },

    //是否显示返回顶部
    contentScroll(position) {
      //1. 判断返回顶部是否显示
      this.isShowBackTop = position.y < 0 ? true : false;
      //2. tabControl是否吸顶
      this.showTabControl = this.offsetTop < -position.y;
    },

    //上拉加载更多
    loadMore() {
      this.fetchHomeGoods(this.currentType);
    },

    //banner图加载完成
    swiperLoad() {
      this.offsetTop = this.$refs.tabControl.$el.offsetTop;
      console.log(this.offsetTop);
    },
  },
};
</script>

<style scoped>
#home {
  height: 100vh;
}
.home-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 9;
   background: rgb(245, 110, 162);
}
.tab-control {
  position: relative;
  top: 44px;
  left: 0;
  right: 0;
  z-index: 99;
}
.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 49px;
  right: 0;
  left: 0;
}
.fixed {
  position: fixed;
  top: 44px;
  left: 0;
  right: 0;
  z-index: 99;
}
/* .content {
  height: calc(100% - 93px);
  overflow: hidden;
  margin-top: 44px;
} */
</style>